package net.liteheaven.xblib.widget.Toast;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TimeInterpolator;
import android.content.Context;
import android.graphics.Point;
import android.view.View;
import android.widget.Toast;

import net.liteheaven.xblib.common.ui.DimensionUtil;

public class MiuiToast extends SmartToast {

    protected MiuiToast(Context context, String text, int duration){
        super(context, text, duration);
        init();
    }

    protected MiuiToast(Context context, View v, int duration){
        super(context, v, duration);
        init();
    }

    private void init(){
        setInterpolatorIndex(8);
    }

    @Override
    protected void onStart(){
        final float maxAlpha = 0.9f;
        final int alphaInterval = 500;
        final int animEnterInterval = 500;
        long interval = (mDuration == Toast.LENGTH_LONG) ? 3500 : 2000;
        if (interval < animEnterInterval){
            interval = animEnterInterval;
        }

        Point sc = DimensionUtil.getScreenSize(mCtx);
        mToastView.measure(0,0);

        int toastHeight = mToastView.getMeasuredHeight();
        int start = sc.y - DimensionUtil.getStatusBarHeight(mCtx) - toastHeight- getYStart();
        int end = start - getYTranslation();

        // 移动动画
        PropertyValuesHolder hTranslate = PropertyValuesHolder.ofInt("offsetY", start, end);
        ObjectAnimator oaTranslate = ObjectAnimator.ofPropertyValuesHolder(this, hTranslate);
        TimeInterpolator tiTranslate = getInterpolator(mInterpolatorIndex);
        oaTranslate.setInterpolator(tiTranslate);
        oaTranslate.setDuration(alphaInterval);

        // alpha动画
        PropertyValuesHolder hAlpha = PropertyValuesHolder.ofFloat("alpha", 0.0f, maxAlpha);
        ObjectAnimator oaAlpha = ObjectAnimator.ofPropertyValuesHolder(this, hAlpha);
        oaAlpha.setDuration(alphaInterval);

        // 一起显示
        AnimatorSet as = new AnimatorSet();
        AnimatorSet.Builder builder = as.play(oaTranslate);
        builder.with(oaAlpha);
        as.start();

        Runnable runnableCancel = new Runnable() {
            @Override
            public void run() {
                cancel();
            }
        };
        mHandler.postDelayed(runnableCancel, interval);

        Runnable runnableAnimLeave = new Runnable() {
            @Override
            public void run() {
                PropertyValuesHolder holderLeave = PropertyValuesHolder.ofFloat("alpha", maxAlpha, 0);
                ObjectAnimator oaLeave = ObjectAnimator.ofPropertyValuesHolder(MiuiToast.this, holderLeave);
                oaLeave.setDuration(alphaInterval);
                oaLeave.start();
                Animator.AnimatorListener al = new Animator.AnimatorListener(){

                    @Override
                    public void onAnimationStart(Animator animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animation) {
                        //cancel();
                    }

                    @Override
                    public void onAnimationCancel(Animator animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animation) {

                    }
                };
                oaLeave.addListener(al);
            }
        };
        mHandler.postDelayed(runnableAnimLeave, interval-alphaInterval-1);
    }
}
